
package cfar.distributed.reduce;

import cfar.actors.Node;
import cfar.ff.data.List;
import cfar.ff.fun.F0;
import cfar.ff.fun.F1;
import cfar.ff.fun.F2;
import cfar.ff.fun.F3;
import cfar.ff.fun.Seq;
import cfar.ff.interfaces.Sequence;

/**
 *
 * @author badi
 */
public class Reduce<S1,S2,A> implements F3< F0<Sequence<S2, Node>>
                                          , F2<A,A,A>
                                          , Sequence<S1, A>
                                          , A
                                          >
{

    public A f( F0<Sequence<S2, Node>> get_nodes
              , F2<A, A, A> reducer
              , Sequence<S1, A> seq
              )
    {
        Sequence<S1,Integer> rs = (Sequence<S1, Integer>) seq.<Integer>empty();
        for(int i = 0; i < seq.length(); i++)
            rs = rs.cons(1);

        return Seq.distRReduce(rs, chooser, get_nodes, reducer, seq);
    }

    F1<List<A>, A> chooser = new F1<List<A>, A>() {

        public A f(List<A> l) {
            return l.head();
        }
    };

}
